package com.haoshuang.zhouzhoubang.utils;

import android.content.Context
import android.graphics.Color
import android.widget.ImageView
import android.widget.TextView
import com.bigkoo.pickerview.builder.OptionsPickerBuilder

import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener

import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.haoshuang.zhouzhoubang.R


import java.text.SimpleDateFormat
import java.util.*

/**
 * Copyright (c)
 * @Description
 * @Author ("")
 * @Copyright Copyright (c)
 * @Date 2019
 * @CreateBy android_studio
 * @Remarks
 */
class  TimeGetData(internal var context: Context) {
    private var pvCustomTime: TimePickerView? = null

    fun getTimeDate(callBack: TimeCallBack) {
        val curDate = Date(System.currentTimeMillis())
        val tim2 = getTime2(curDate).split("-")
        val year = tim2[0]
        val ma = tim2[1]

        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        val selectedDate = Calendar.getInstance()//系统当前时间
        selectedDate.set(year.toInt(), ma.toInt()-1,1)
        val startDate = Calendar.getInstance()
        startDate.set(1900, 1,1)
        val endDate = Calendar.getInstance()
        endDate.set(year.toInt()+1, 11, 31)
        //时间选择器 ，自定义布局
        pvCustomTime = TimePickerBuilder(context, OnTimeSelectListener { date, v ->
            //选中事件回调
            val time = getTime2(date)
            callBack.onSuccess(time)
        })

                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.pickerview_custom_time) { v ->
                    val tvSubmit = v.findViewById(R.id.tv_finish) as TextView
                    val ivCancel = v.findViewById(R.id.iv_cancel) as TextView
                    tvSubmit.setOnClickListener {
                        pvCustomTime?.returnData()
                        pvCustomTime?.dismiss()
                    }
                    ivCancel.setOnClickListener { pvCustomTime?.dismiss() }
                }
                .setContentTextSize(18)

                .setType(booleanArrayOf(true, true, false, false, false, false))
                .setLabel("年", "月", "日", "时", "分", "秒")
                .setLineSpacingMultiplier(2.5f)
                .setTextXOffset(0, 0, 0, 40, 0, -40)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                //  .setDividerColor(-0xdb5263)
                .build()
        pvCustomTime?.show()
    }

    open fun Choose(options1Items: ArrayList<String>, tv_fx: TextView?,callBack:TimeCallBack,str:String) {
        val pvNoLinkOptions = OptionsPickerBuilder(context, OnOptionsSelectListener { options1, options2, options3, v ->
            tv_fx?.text = options1Items[options1]
            callBack.onSuccess(options1.toString())

        })
            .setOptionsSelectChangeListener { options1, options2, options3 ->
                // callBack.onSuccess(options1.toString())
            }
            // .setSelectOptions(0, 1, 1)
            .setTitleText(str)
            .setSubmitColor(Color.parseColor("#07C160"))
            .setCancelColor(Color.parseColor("#666666"))
            .setCancelText("取消")
            .setSubmitText("确定")
            .build<String>()
        pvNoLinkOptions?.setNPicker(options1Items, null, null)
        pvNoLinkOptions.show()
    }

    private fun getTime2(date: Date): String {//可根据需要自行截取数据显示
        val format = SimpleDateFormat("yyyy-MM")
        return format.format(date)
    }
    private fun getTime(date: Date): String {//可根据需要自行截取数据显示
        val format = SimpleDateFormat("yyyy.MM.dd")
        return format.format(date)
    }
}

